package com.ruoyi.web.controller.sj.controller;

import java.util.List;

import com.ruoyi.framework.web.exception.base.XResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sj.domain.Message;
import com.ruoyi.sj.service.IMessageService;
import com.ruoyi.web.core.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;

/**
 * 消息通知 信息操作处理
 *
 * @author tyc@yanyu.com
 * @date 2018-11-28
 */
@Controller
@RequestMapping("/sj/message")
public class MessageController extends BaseController {
    private String prefix = "sj/message";

    @Autowired
    private IMessageService messageService;

    @RequiresPermissions("sj:message:view")
    @GetMapping()
    public String message() {
        return prefix + "/message";
    }

    /**
     * 查询消息通知列表
     */
    @RequiresPermissions("sj:message:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Message message) {
        startPage();
        List<Message> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }

    /**
     * 新增消息通知
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存消息通知
     */
    @RequiresPermissions("sj:message:add")
    @Log(title = "消息通知", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Message message) {
        return toAjax(messageService.insertMessage(message));
    }

    /**
     * 修改消息通知
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Message message = messageService.selectMessageById(id);
        mmap.put("message", message);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息通知
     */
    @RequiresPermissions("sj:message:edit")
    @Log(title = "消息通知", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Message message) {
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 删除消息通知
     */
    @RequiresPermissions("sj:message:remove")
    @Log(title = "消息通知", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(messageService.deleteMessageByIds(ids));
    }

    /**
     * 后台消息推送
     * @param id
     * @return
     */
    @RequiresPermissions("sj:message:edit")
    @PostMapping("/pushMessage")
    @ResponseBody
    public XResult pushMessage(String id){
        return new XResult("操作成功",messageService.pushMessage(id));
    }

}
